package com.learn.file.controller;


import com.learn.file.entity.UploadFile;
import com.learn.file.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YU.TAN
 * @since 2022-11-14
 */
@Controller
@RequestMapping("/upload-file")
public class UploadFileController {

    @Autowired
    private UploadFileService fileService;

    @GetMapping("/index")
    public String toIndex(Model model) {
        List<UploadFile> list = fileService.list();
        model.addAttribute("fileList",list);
        return "index";
    }



    @PostMapping("/upload")
    public String upload(MultipartFile multipartFile) throws Exception {
        fileService.upload(multipartFile);
        return "redirect:/upload-file/index";
    }

    @GetMapping("/download")
    public void download(Integer id, HttpServletResponse response) throws Exception {
        fileService.download(id,response);
    }


    @GetMapping("/delete")
    public String delete(int id) throws Exception {
        fileService.delete(id);
        return "redirect:/upload-file/index";
    }

}
